package com.fms.trollalarm.fragment;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

import com.fms.trollalarm.R;
import com.fms.trollalarm.object.LevelType;
import com.fms.trollalarm.object.TrollType;
import com.fms.trollalarm.utils.Common;
import com.fms.trollalarm.utils.LevelSetting.Easy;
import com.fms.trollalarm.utils.LevelSetting.Hard;
import com.fms.trollalarm.utils.LevelSetting.Medium;

public class RememberMeFragment extends AlarmFragment implements ViewFactory {
	private TextSwitcher tsRandomText;
	private TextView tvTitle;
	private LinearLayout llAnswer;
	private Button btChange;
	private EditText etAnswer;
	private Button btAnswer;
	private Button btSnooze;
	private Button btStart;
	private int typeAnswer;
	private Random rnd = new Random();
	private String firstToLast;
	private String lastToFirst;
	private String answer;
	private int size = 5;
	private int count = 0;

	public static RememberMeFragment newInstance(int levelType) {
		RememberMeFragment fragment = new RememberMeFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(Common.IntentExtra.TROLL_LEVEL, levelType);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_remember_me, null);
		findViewIDs(v);
		init();
		setEventView();
		return v;
	}

	private void setEventView() {
		btStart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startMission();
			}
		});
		btSnooze.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnSnoozeAlarm();
			}
		});
		btAnswer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (etAnswer.getText().toString().equalsIgnoreCase(answer)) {
					setOnStopAlarm();
				}

			}
		});

		btChange.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnChangeTrollType(TrollType.REMEMBER_ME);
			}
		});
	}

	private void init() {
		setTrollLevel();
		tsRandomText.setFactory(this);
		tsRandomText.setText(createEmptyText(size));
		firstToLast = getResources().getString(
				R.string.title_remember_first_last);
		lastToFirst = getResources().getString(
				R.string.title_remember_last_first);
	}

	private void setTrollLevel() {
		int trollLevel = getTrollLevel();
		if (trollLevel == LevelType.EASY.ordinal()) {
			size = Easy.REMEMBER_ME;
		} else if (trollLevel == LevelType.MEDIUM.ordinal()) {
			size = Medium.REMEMBER_ME;
		} else {
			size = Hard.REMEMBER_ME;
		}
	}

	private void startMission() {
		tvTitle.setText("");
		tsRandomText.setText("");
		typeAnswer = rnd.nextInt(2);
		answer = randomText(size);
		showText();
	}

	private void showText() {
		count = 0;
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				tsRandomText.post(new Runnable() {

					@Override
					public void run() {
						if (count < answer.length()) {
							tsRandomText.setText(createEmptyText(count)
									+ String.valueOf(answer.charAt(count))
									+ createEmptyText(answer.length() - count));
							count++;
						} else {
							if (typeAnswer == 0) {
								tvTitle.setText(firstToLast);
								timer.cancel();
							} else {
								tvTitle.setText(lastToFirst);
								answer = new StringBuilder(answer).reverse()
										.toString();
								timer.cancel();
							}

						}

					}
				});

			}
		}, 0, 500);
	}

	private String createEmptyText(int length) {
		String text = "";
		for (int i = 0; i < length; i++) {
			text = text + "  ";
		}
		return text;
	}

	public String randomText(int size) {
		String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		char[] text = new char[size];
		for (int i = 0; i < size; i++) {
			int random = rnd.nextInt(characters.length());
			char mChar = characters.charAt(random);
			text[i] = mChar;
			StringBuilder newString = new StringBuilder(characters)
					.deleteCharAt(random);
			characters = newString.toString();
		}
		return new String(text);
	}

	private void findViewIDs(View v) {
		tsRandomText = (TextSwitcher) v.findViewById(R.id.ts_random_text);
		tvTitle = (TextView) v.findViewById(R.id.tv_title);
		llAnswer = (LinearLayout) v.findViewById(R.id.ll_answer);
		etAnswer = (EditText) v.findViewById(R.id.et_answer);
		btAnswer = (Button) v.findViewById(R.id.bt_answer);
		btSnooze = (Button) v.findViewById(R.id.bt_snooze);
		btStart = (Button) v.findViewById(R.id.bt_start);
		btChange = (Button) v.findViewById(R.id.bt_change);
	}

	@Override
	public View makeView() {
		TextView t = new TextView(getActivity());
		t.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL);
		t.setTextSize(50);
		t.setTextColor(Color.RED);
		return t;
	}

}
